<template>
  <div>
    <!-- 月度结算查询-->
    <TopFilter ref="topform" :list="topFilterList" :cols="3" @filterChange="changeHandle" @onCollapse="collapseHandle" :labelWidth="110" @resetChange="resetChange"></TopFilter>
    <FilterTables
      ref="table"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :isSelectColumn="false"
      :datakey="'records'"
      columnsRef="MonthlySalesQuery"
      :onColumnsChange="(columns) => (this.columns = columns)"
    >
      <template slot="controls" slot-scope="props">
        <multiuse-button size="small" type="primary" @click="addHandle(1)">导 出</multiuse-button>
      </template>
    </FilterTables>
  </div>
</template>
<script>
import { getLevelOneArea } from '@/api/cms-common/dealerdata/index';
import { region } from '@/api/cms-common/region';
import { getBrandListOEM, getSeriesListOEM, getModelListOEM, getPackageListOEM, getOptionListDictOEM, getWcolorList, getIcolorList } from '@/api/common/cars/masterDataCars';
import { getList, exporturl, exporList } from '@/api/omSaleP/OrderManagement/MonthlySales/index.js';
import { dictionary } from '@/utils/dictMixin';
import { exportExcelFile } from '@/utils';
import { dealerMixin } from '@/pages/repair/batchEventManagement/dealerMixin';
import moment from 'moment';
export default {
  name: 'MonthlySalesQuery',
  data() {
    this.BaseTable = null;
    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),
      fetchApi: getList,
      fetchParams: {},
    };
  },
  mixins: [dictionary, dealerMixin, region],
  components: {},
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
    //初始化所属区域数据
    this.getTopLevelArea();
    //初始化省份数据
    this.initProvince();
  },
  created() {
    //初始化品牌数据
    this.getDictBrandList();
    //初始化外饰数据
    this.getColors();
    //初始化内饰数据
    this.getIColors();
  },
  methods: {
    // 所属区域
    async getTopLevelArea() {
      const res = await getLevelOneArea(15061015);
      if (res.resultCode === 200) {
        this.topFilterList.find((item) => item.fieldName === 'areaId').itemList = res.data.map((x) => ({ value: parseInt(x.id), text: x.orgName }));
      }
    },
    //初始化所在省份
    async initProvince() {
      let pl = await this.createRegionList();
      this.topFilterList.find((item) => item.fieldName === 'provinceId').itemList = pl;
    },
    //初始化所在市数据
    async initCity(provinceId) {
      this.topFilterList.find((item) => item.fieldName == 'cityId').initialValue = '';
      this.topFilterList.find((item) => item.fieldName == 'cityId').itemList = [];
      if (!provinceId || provinceId == null || typeof provinceId == 'undefined') {
        return;
      }
      //取省数据
      let rd = this.getRegionData();
      this.cityData = rd[provinceId].children;
      let cl = this.createSubRegionList(this.cityData);
      //初始化市，清除原值
      let city = this.topFilterList.find((item) => item.fieldName === 'cityId');
      city.itemList = cl;
      city.initialValue = '';
    },
    //查询品牌数据
    async getDictBrandList() {
      const res = await getBrandListOEM();
      if (res.resultCode === 200) {
        // console.log(this.topFilterList, 'this.topFilterList');
        this.topFilterList.find((item) => item.fieldName === 'brandId').itemList = res.data.map((x) => ({ value: x.brandId, text: x.brandName }));
      }
    },
    //查询外饰数据
    async getColors() {
      const res = await getWcolorList();
      if (res.resultCode === 200) {
        this.topFilterList.find((item) => item.fieldName === 'colorId').itemList = res.data.map((x) => ({ value: x.colorId, text: x.colorName }));
      }
    },
    //查询内饰数据
    async getIColors() {
      const res = await getIcolorList();
      if (res.resultCode === 200) {
        this.topFilterList.find((item) => item.fieldName === 'trimColorId').itemList = res.data.map((x) => ({ value: x.colorId, text: x.colorName }));
      }
    },

    //修改品牌下拉查询条件
    async changeBrandSelect(brandsid) {
      this.topFilterList.find((item) => item.fieldName === 'seriesId').initialValue = '';
      this.topFilterList.find((item) => item.fieldName === 'seriesId').itemList = [];
      this.topFilterList.find((item) => item.fieldName === 'modelId').initialValue = '';
      this.topFilterList.find((item) => item.fieldName === 'modelId').itemList = [];
      this.topFilterList.find((item) => item.fieldName === 'packageId').initialValue = '';
      this.topFilterList.find((item) => item.fieldName === 'packageId').itemList = [];
      this.topFilterList.find((item) => item.fieldName === 'optionId').initialValue = '';
      this.topFilterList.find((item) => item.fieldName === 'optionId').itemList = [];
      if (brandsid) {
        const res = await getSeriesListOEM(brandsid);
        if (res.resultCode === 200) {
          this.topFilterList.find((item) => item.fieldName === 'seriesId').itemList = res.data.map((x) => ({ value: x.seriesId, text: x.seriesName }));
        }
      }
    },
    //修改车系下拉查询条件
    async changeSeriesSelect(seriessid) {
      const brandsid = this.topFilterList.find((item) => item.fieldName === 'brandId').initialValue;
      if (brandsid && seriessid) {
        this.topFilterList.find((item) => item.fieldName === 'modelId').initialValue = '';
        this.topFilterList.find((item) => item.fieldName === 'modelId').itemList = [];
        this.topFilterList.find((item) => item.fieldName === 'packageId').initialValue = '';
        this.topFilterList.find((item) => item.fieldName === 'packageId').itemList = [];
        this.topFilterList.find((item) => item.fieldName === 'optionId').initialValue = '';
        this.topFilterList.find((item) => item.fieldName === 'optionId').itemList = [];
        const res = await getModelListOEM(brandsid, seriessid);
        if (res.resultCode === 200) {
          this.topFilterList.find((item) => item.fieldName === 'modelId').itemList = res.data.map((x) => ({ value: x.modelId, text: x.modelName }));
        }
      }
    },
    //修改车型下拉查询条件
    async changeModelSelect(modelId) {
      const brandsid = this.topFilterList.find((item) => item.fieldName === 'brandId').initialValue;
      const seriessid = this.topFilterList.find((item) => item.fieldName === 'seriesId').initialValue;
      this.topFilterList.find((item) => item.fieldName === 'packageId').initialValue = '';
      this.topFilterList.find((item) => item.fieldName === 'packageId').itemList = [];
      this.topFilterList.find((item) => item.fieldName === 'optionId').initialValue = '';
      this.topFilterList.find((item) => item.fieldName === 'optionId').itemList = [];
      if (modelId) {
        const res = await getPackageListOEM(brandsid, seriessid, modelId);
        if (res.resultCode === 200) {
          this.topFilterList.find((item) => item.fieldName === 'packageId').itemList = res.data.map((x) => ({ value: x.packageId, text: x.configName }));
        }
      }
    },
    //修改配置下拉查询条件
    async changePackageSelect(packageId) {
      const brandsid = this.topFilterList.find((item) => item.fieldName === 'brandId').initialValue;
      const seriessid = this.topFilterList.find((item) => item.fieldName === 'seriesId').initialValue;
      this.topFilterList.find((item) => item.fieldName === 'optionId').initialValue = '';
      this.topFilterList.find((item) => item.fieldName === 'optionId').itemList = [];
      if (packageId) {
        const res = await getOptionListDictOEM(packageId);
        if (res.resultCode === 200) {
          this.topFilterList.find((item) => item.fieldName === 'optionId').itemList = res.data.map((x) => ({ value: x.optionId, text: x.optionName }));
        }
      }
    },
    //获取经销商资金类型
    async getAllDealerAccountTypeData() {
      let res = (await getAllDealerAccountType()).data.map((x) => ({ text: x.accountTypeName, value: x.accountId }));
      this.topFilterList.find((x) => x.fieldName == 'accountId').itemList = res;
    },
    async addHandle(type) {
      // let json = Object.assign({}, { CODE: 'CODE_0000006' }, this.fetchParams);
      // let res = await exporturl(json);
      // if (res.resultCode == 200) {
      //   this.$router.push('/admin/auth/oem/downloadCenter');
      // }

      this.$confirm('是否将Excel表格保存至本地文件夹?', '提示', {
        confirmButtonText: '下载',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          this.$refs.topform.SUBMIT_FORM();
          let json = this.fetchParams;
          exporList(json)
            .then((response) => {
              exportExcelFile(response.data, '月度结算查询');
            })
            .catch((err) => {
              // console.log(err);
            });
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消',
          });
        });
    },
    //查询条件时间限制
    changeHandle(params) {
      // if (this.checkTime(params.firstFinanceCheckStartDate, params.firstFinanceCheckEndDate, 24)) {
      //   return this.$notify.warning({ title: '提示信息', message: '起止日期跨度不能超过两年' });
      // }
      // if (this.checkTime(params.commandStartDate, params.commandEndDate, 24)) {
      //   return this.$notify.warning({ title: '提示信息', message: '起止日期跨度不能超过两年' });
      // }
      // if (this.checkTime(params.salesReturnStartData, params.salesReturnEndData, 24)) {
      //   return this.$notify.warning({ title: '提示信息', message: '起止日期跨度不能超过两年' });
      // }
      // if (this.checkTime(params.salesReturnStartData, params.salesReturnEndData, 24)) {
      //   return this.$notify.warning({ title: '提示信息', message: '起止日期跨度不能超过两年' });
      // }
      this.fetchParams = Object.assign({}, this.fetchParams, params);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    resetChange(val) {
      this.fetchParams = Object.assign({}, this.fetchParams);
    },
    createdFrom() {
      return [
        {
          type: 'TEXT_AREA',
          label: '结算订单编号',
          fieldName: 'doNo',
          maxlength: '10000',
          placeholder: '输入多个结算订单编号时，请用英文逗号或者换行隔开（例如：单号1,单号2……）',
          maxRows: 2,
        },
        {
          type: 'INPUT',
          label: '订单编号',
          fieldName: 'orderNo',
          placeholder: '请输入',
        },
        {
          type: 'SELECT',
          label: '订单类型',
          fieldName: 'orderType',
          placeholder: '请选择',
          itemList: this.createDictList('2105'),
        },
        {
          type: 'TEXT_AREA',
          label: 'VIN',
          fieldName: 'vin',
          placeholder: '输入多个VIN时，请用英文逗号或者换行隔开（例如：VIN1,VIN2……）',
          maxlength: '10000',
          maxRows: 2,
        },
        // {
        //   type: 'SELECT',
        //   label: '订单明细状态',
        //   fieldName: 'orderStatus',
        //   placeholder: '请选择',
        //   itemList: this.createDictList('2109'),
        // },
        {
          type: 'INPUT',
          label: '经销商代码',
          fieldName: 'dealerCode',
          placeholder: '输入多个经销商代码时，请用英文逗号隔开',
        },
        {
          type: 'INPUT',
          label: '经销商名称',
          fieldName: 'dealerName',
          placeholder: '请输入',
        },
        {
          type: 'SELECT',
          label: '所属区域',
          fieldName: 'areaId',
          placeholder: '请选择',
          itemList: [],
        },
        {
          type: 'SELECT',
          label: '省份',
          fieldName: 'provinceId',
          placeholder: '请选择',
          initialValue: '',
          itemList: [],
          change: (props) => this.initCity(props),
        },
        {
          type: 'SELECT',
          label: '城市',
          fieldName: 'cityId',
          placeholder: '请选择',
          initialValue: '',
          itemList: [],
        },
        {
          type: 'SELECT',
          label: '品牌',
          fieldName: 'brandId',
          placeholder: '请选择',
          itemList: [],
          change: (val) => {
            this.changeBrandSelect(val);
          },
        },
        {
          type: 'SELECT',
          label: '车系',
          fieldName: 'seriesId',
          placeholder: '请选择',
          itemList: [],
          change: (val) => {
            this.changeSeriesSelect(val);
          },
        },
        {
          type: 'SELECT',
          label: '车型',
          fieldName: 'modelId',
          placeholder: '请选择',
          itemList: [],
          change: (val) => {
            this.changeModelSelect(val);
          },
        },
        {
          type: 'SELECT',
          label: '配置',
          fieldName: 'packageId',
          placeholder: '请选择',
          itemList: [],
          change: (val) => {
            this.changePackageSelect(val);
          },
        },
        {
          type: 'SELECT',
          label: '选装',
          fieldName: 'optionId',
          placeholder: '请选择',
          itemList: [],
        },
        {
          type: 'SELECT',
          label: '外饰色',
          fieldName: 'colorId',
          placeholder: '请选择',
          itemList: [],
        },
        {
          type: 'SELECT',
          label: '内饰色',
          fieldName: 'trimColorId',
          placeholder: '请选择',
          itemList: [],
        },
        {
          type: 'INPUT',
          label: '物料编码',
          fieldName: 'productCode',
          placeholder: '请输入',
        },
        {
          type: 'INPUT',
          label: '物料描述',
          fieldName: 'productName',
          placeholder: '请输入',
        },
        {
          type: 'RANGE_DATE',
          label: '出库时间',
          fieldName: 'sapOutStartData|sapOutEndData',
          placeholder: '请选择',
          initialValue: [],
          dateType: 'exactdaterange',
        },
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: (props) => {
            return <span>{props.row.index + 1}</span>;
          },
        },
        {
          title: '所属区域',
          dataIndex: 'areaName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '省份',
          dataIndex: 'provinceName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '城市',
          dataIndex: 'cityName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '经销商代码',
          dataIndex: 'dealerCode',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '经销商名称',
          dataIndex: 'dealerName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: 'VIN',
          dataIndex: 'vin',
          width: 200,
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '订单编号',
          dataIndex: 'orderNo',
          sorter: true,
          filter: true,
          filterType: 'input',
          width: 200,
        },
        {
          title: '结算订单编号',
          dataIndex: 'doNo',
          sorter: true,
          filter: true,
          filterType: 'input',
          width: 200,
        },
        {
          title: '采购退货单号',
          dataIndex: 'salesReturnNo',
          sorter: true,
          filter: true,
          width: 200,
          filterType: 'input',
        },
        {
          title: '订单年月',
          dataIndex: 'orderYearMonth',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '订单类型',
          dataIndex: 'orderType',
          sorter: true,
          filter: true,
          filterType: 'input',
          render: (p) => {
            return <span>{this.createDictText(p.row.orderType, '2105')}</span>;
          },
        },
        {
          title: '车辆用途',
          dataIndex: 'vehicleUse',
          sorter: true,
          filter: true,
          filterType: 'input',
          render: (p) => {
            return <span>{this.createDictText(p.row.vehicleUse, '2102')}</span>;
          },
        },
        {
          title: '订单明细状态',
          dataIndex: 'orderStatus',
          sorter: true,
          filter: true,
          filterType: 'input',
          render: (p) => {
            return <span>{this.createDictText(p.row.orderStatus, '2109')}</span>;
          },
        },
        {
          title: '大客户编号',
          dataIndex: 'customersNo',
          sorter: true,
          filter: true,
          width: 200,
          filterType: 'input',
        },
        {
          title: '运输方式',
          dataIndex: 'transportType',
          sorter: true,
          filter: true,
          filterType: 'input',
          render: (p) => {
            return <span>{this.createDictText(p.row.transportType, '2409')}</span>;
          },
        },
        // {
        //   title: '资金类型',
        //   dataIndex: 'accountTypeName',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input'
        // },
        {
          title: '收车地址',
          dataIndex: 'address',
          sorter: true,
          filter: true,
          width: 200,
          filterType: 'input',
        },
        // {
        //   title: '联系人',
        //   dataIndex: 'linkMan',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input'
        // },
        // {
        //   title: '联系电话',
        //   dataIndex: 'linkPhone',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input'
        // },
        {
          title: '分配数量',
          dataIndex: 'distributionNumber',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '物料编码',
          dataIndex: 'productCode',
          sorter: true,
          filter: true,
          width: 200,
          filterType: 'input',
        },
        {
          title: '物料描述',
          dataIndex: 'productName',
          width: 400,
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '品牌',
          dataIndex: 'brandName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '车系',
          dataIndex: 'seriesName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '车型',
          dataIndex: 'modelName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '配置',
          dataIndex: 'configName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '选装',
          dataIndex: 'optionName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '内饰',
          dataIndex: 'trimName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '外饰',
          dataIndex: 'colorName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '市场指导价',
          dataIndex: 'singleAmount',
          sorter: true,
          filter: true,
          filterType: 'input',
          precision: 2,
          numberFormat: true,
          align: 'right',
        },
        {
          title: '采购价格',
          dataIndex: 'afterDiscountAmount',
          sorter: true,
          filter: true,
          filterType: 'input',
          precision: 2,
          numberFormat: true,
          align: 'right',
        },
        {
          title: '现金折让',
          dataIndex: 'discountAmount',
          sorter: true,
          filter: true,
          filterType: 'input',
          precision: 2,
          numberFormat: true,
          align: 'right',
        },
        {
          title: '建店返利',
          dataIndex: 'buildRebateAmount',
          sorter: true,
          filter: true,
          filterType: 'input',
          precision: 2,
          numberFormat: true,
          align: 'right',
        },
        {
          title: '是否使用返利',
          dataIndex: 'isRebate',
          sorter: true,
          filter: true,
          filterType: 'intput',
          render: (p) => {
            return <span>{this.createDictText(p.row.isRebate, '1004')}</span>;
          },
        },
        {
          title: '返利冲抵',
          dataIndex: 'singleRebateAmount',
          sorter: true,
          filter: true,
          filterType: 'input',
          precision: 2,
          numberFormat: true,
          align: 'right',
        },
        {
          title: '提报时间',
          dataIndex: 'reportDate',
          sorter: true,
          filter: true,
          width: 150,
          dateFormat: 'yyyy-MM-dd HH:mm:ss',
          filterType: 'date-range',
        },
        {
          title: '业务审核时间',
          dataIndex: 'checkDate',
          sorter: true,
          filter: true,
          width: 150,
          dateFormat: 'yyyy-MM-dd HH:mm:ss',
          filterType: 'date-range',
        },
        //  {
        //   title: '审核人',
        //   dataIndex: 'checkPersonName',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input'
        // },
        // {
        //   title: '首次业务审核时间',
        //   dataIndex: 'firstCheckDate',
        //   sorter: true,
        //   filter: true,
        //   width: 150,
        //   dateFormat: 'yyyy-MM-dd HH:mm:ss',
        //   filterType: 'date-range'
        // },
        // {
        //   title: '首次财务结算时间',
        //   dataIndex: 'firstFinanceCheckDate',
        //   sorter: true,
        //   filter: true,
        //   width: 150,
        //   dateFormat: 'yyyy-MM-dd HH:mm:ss',
        //   filterType: 'date-range'
        // },
        {
          title: '财务审核时间',
          dataIndex: 'financeCheckDate',
          sorter: true,
          filter: true,
          width: 150,
          dateFormat: 'yyyy-MM-dd HH:mm:ss',
          filterType: 'date-range',
        },
        {
          title: '出库时间',
          dataIndex: 'sapOutDate',
          sorter: true,
          filter: true,
          width: 150,
          dateFormat: 'yyyy-MM-dd HH:mm:ss',
          filterType: 'date-range',
        },
        {
          title: '发运时间',
          dataIndex: 'commandDate',
          sorter: true,
          filter: true,
          width: 150,
          dateFormat: 'yyyy-MM-dd HH:mm:ss',
          filterType: 'date-range',
        },
        {
          title: '采购退货时间',
          dataIndex: 'salesReturnDate',
          sorter: true,
          filter: true,
          width: 150,
          dateFormat: 'yyyy-MM-dd HH:mm:ss',
          filterType: 'date-range',
        },
        // {
        //   title: '经销商收车时间',
        //   dataIndex: 'receiveDate',
        //   sorter: true,
        //   filter: true,
        //  dateFormat: 'yyyy-MM-dd HH:mm:ss',
        //   filterType: 'date-range'
        // }
      ];
    },
  },
};
</script>
